package com.demo.supplier.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.demo.supplier.common.R;
import com.demo.supplier.entity.SupplierInfo;
import com.demo.supplier.entity.SysUser;
import com.demo.supplier.mapper.SupplierInfoMapper;
import com.demo.supplier.mapper.SysUserMapper;
import com.demo.supplier.service.LogService;
import com.demo.supplier.vo.DashboardStatsVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@Slf4j
@RestController
@RequestMapping("${api.prefix}" + "/dashboard")
@Api(tags = "首页统计")
public class DashboardController {

    @Autowired
    private SysUserMapper sysUserMapper;

    @Autowired
    private SupplierInfoMapper supplierInfoMapper;

    @Autowired
    private LogService logService;

    @GetMapping("/stats")
    @ApiOperation("获取首页统计数据")
    public R<DashboardStatsVo> getStats() {
        DashboardStatsVo vo = new DashboardStatsVo();

        // 用户总数（未删除）
        long userCount = sysUserMapper.selectCount(new LambdaQueryWrapper<SysUser>().eq(SysUser::getDeleted, 0));
        vo.setUserCount(userCount);

        // 供应商总数（未删除）
        long supplierCount = supplierInfoMapper.selectCount(new LambdaQueryWrapper<SupplierInfo>().eq(SupplierInfo::getDeleted, 0));
        vo.setSupplierCount(supplierCount);

        // 活跃供应商（status=1 且未删除）
        long activeSupplierCount = supplierInfoMapper.selectCount(new LambdaQueryWrapper<SupplierInfo>()
                .eq(SupplierInfo::getDeleted, 0)
                .eq(SupplierInfo::getStatus, 1));
        vo.setActiveSupplierCount(activeSupplierCount);

        // 今日操作日志数（Mongo 按 createTime 当天范围统计）
        long todayLogCount = logService.countTodayOperationLogs();
        vo.setTodayLogCount(todayLogCount);

        return R.success(vo);
    }
}


